package org.jeecg.modules.demo.cashier;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.api.UpmsHelloApi;
import org.jeecg.modules.demo.entity.Dept;
import org.jeecg.modules.demo.mall.entity.YxShopProduct;
import org.jeecg.modules.demo.mall.entity.YxShopSku;
import org.jeecg.modules.demo.mall.entity.YxStoreCategory;
import org.jeecg.modules.demo.mall.yxlevelproductdiscount.service.IYxLevelProductDiscountService;
import org.jeecg.modules.demo.mall.yxshopproduct.service.IYxShopProductService;
import org.jeecg.modules.demo.mall.yxshopsku.service.IYxShopSkuService;
import org.jeecg.modules.demo.mall.yxstorecategory.service.IYxStoreCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: yx_shop_product
 * @Author: jeecg-boot
 * @Date: 2023-10-23
 * @Version: V1.0
 */
@Api(tags = "收银台商家商品查询")
@RestController
@RequestMapping("/mall/cashiershopproduct/cashierShopProduct")
@Slf4j
public class CashierShopProductController extends JeecgController<YxShopProduct, IYxShopProductService> {
    @Autowired
    private IYxShopProductService yxShopProductService;
    @Autowired
    private IYxLevelProductDiscountService yxLevelProductDiscountService;
    @Autowired
    private IYxStoreCategoryService yxStoreCategoryService;
    @Autowired
    private UpmsHelloApi upmsHelloApi;
    @Autowired
    private IYxShopSkuService yxShopSkuService;

    /**
     * 商家商品查询
     *
     * @param yxShopProduct
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "商家商品查询", notes = "商家商品查询")
    @GetMapping(value = "/queryShopProductPage")
    public Result<IPage<YxShopProduct>> queryPageList(YxShopProduct yxShopProduct,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize) {
        if (yxShopProduct.getMerId() == null) {
            return Result.error("商家id为空!");
        }
        QueryWrapper<YxShopProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0);
        queryWrapper.eq("mer_id", yxShopProduct.getMerId());
        if (yxShopProduct.getExamineStatus() != null) {
            queryWrapper.eq("examine_status", yxShopProduct.getExamineStatus());
        }
        if (StringUtils.isNoneBlank(yxShopProduct.getCateId())){
            queryWrapper.eq("cate_id",yxShopProduct.getCateId());
        }
        if (StringUtils.isNotBlank(yxShopProduct.getStoreName())) {
            queryWrapper.like("store_name", yxShopProduct.getStoreName());
        }
        queryWrapper.orderByDesc("sort");
        Page<YxShopProduct> page = new Page<YxShopProduct>(pageNo, pageSize);
        IPage<YxShopProduct> pageList = yxShopProductService.page(page, queryWrapper);
        if (CollectionUtils.isEmpty(pageList.getRecords())) {
            return Result.ok(pageList);
        }
        pageList.getRecords().forEach(yxShopProduct1 -> {
            YxStoreCategory categoryServiceById = yxStoreCategoryService.getById(Integer.valueOf(yxShopProduct1.getCateId()));
            if (categoryServiceById != null) {
                yxShopProduct1.setCateName(categoryServiceById.getCateName());
            }
            Dept dept = upmsHelloApi.queryByDept(Long.valueOf(yxShopProduct1.getMerId()));
            if (dept != null) {
                yxShopProduct1.setMerName(dept.getName());
            }
            yxShopProduct1.setSkus(yxShopSkuService.querySkuList(yxShopProduct1.getId()));
        });
        //后期需要本店商品绑定   现在平台是以跃动商品为主
//        List<YxShopProduct> yxShopProductList = pageList.getRecords().stream().map(item -> {
//            YxLevelProductDiscount yxLevelProductDiscount = yxLevelProductDiscountService.queryLevelPrice(1, yxShopProduct.getLevelId(), yxShopProduct.getMerId(), item.getId());
//            if (yxLevelProductDiscount != null) {
//                item.setLevelPrice(item.getPrice().multiply(yxLevelProductDiscount.getDiscount()));
//            }
//            return item;
//        }).collect(Collectors.toList());
//        return Result.OK(pageList.setRecords(yxShopProductList));
        return Result.OK(pageList);
    }


    /**
     * 商家商品添加添加
     *
     * @param yxShopProduct
     * @return
     */
    @ApiOperation(value = "商家商品-添加", notes = "商家商品-添加")
    @PostMapping(value = "/shopProductAdd")
    public Result<String> add(@RequestBody YxShopProduct yxShopProduct) {
        yxShopProduct.setExamineStatus(2);
        yxShopProductService.save(yxShopProduct);
        YxShopSku yxShopSku = new YxShopSku();
        yxShopSku.setMerId(yxShopProduct.getMerId());
        yxShopSku.setMerName(yxShopProduct.getMerName());
        yxShopSku.setStoreId(yxShopProduct.getId());
        yxShopSku.setStoreName(yxShopProduct.getStoreName());
        yxShopSku.setStock(yxShopProduct.getStock());
        yxShopSku.setSkuName(yxShopProduct.getStoreName());
        yxShopSku.setSkuPrice(yxShopProduct.getPrice());
        yxShopSku.setVipPrice(yxShopProduct.getVipPrice());
        yxShopSkuService.save(yxShopSku);
        return Result.OK("添加成功！");
}


    /**
     * 编辑
     *
     * @param yxShopProduct
     * @return
     */
    @ApiOperation(value = "yx_shop_product-编辑", notes = "yx_shop_product-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody YxShopProduct yxShopProduct) {
        YxShopSku yxShopSku = new YxShopSku();
        yxShopSku.setId(yxShopProduct.getSkuId());
        yxShopSku.setMerId(yxShopProduct.getMerId());
        yxShopSku.setMerName(yxShopProduct.getMerName());
        yxShopSku.setStoreId(yxShopProduct.getId());
        yxShopSku.setStoreName(yxShopProduct.getStoreName());
        yxShopSku.setStock(yxShopProduct.getStock());
        yxShopSku.setSkuName(yxShopProduct.getStoreName());
        yxShopSku.setSkuPrice(yxShopProduct.getPrice());
        yxShopSku.setVipPrice(yxShopProduct.getVipPrice());
        yxShopSkuService.saveOrUpdate(yxShopSku);
        yxShopProductService.updateById(yxShopProduct);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "yx_shop_product-通过id删除", notes = "yx_shop_product-通过id删除")
    @GetMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) Integer id) {
        yxShopProductService.removeById(id);
        return Result.OK("删除成功!");
    }


}
